<?php

class NhatKyCuaBan_IndexController extends BaseController
{

	var $_current_member_id;
	var $_link_add_url;
	var $_link_add_comment_url;
	var $_default_cat_id;
	
	public function __construct()
	{
		parent::__construct();
		$this->_children[] = new Request('trang-chu/index/nav-header',array('nhat-ky-cua-ban'));

		$this->_current_member_id = h($_SESSION["fb_user"]['id']);//1;
		$this->_link_add_url = site_url('nhat-ky-cua-ban/index/add-question');
		$this->_link_add_comment_url = site_url('nhat-ky-cua-ban/index/add-comment');
		
		$this->oView->current_member_id = $this->_current_member_id;
		$this->oView->link_add_url = $this->_link_add_url;
		$this->oView->link_add_comment_url = $this->_link_add_comment_url;
		
		$objCategory = new Amway_Category();
		$rowCategory = $objCategory->getRow("cat_default = 1 && active = 1");
		$this->_default_cat_id = $rowCategory['id'];
		$this->oView->cat_id = $this->_default_cat_id; 		
	}

	public function indexAction() 
	{
		$this->cauHoiCuaBanAction();
	}
	
	public function cauHoiCuaBanAction()
	{
		$this->oView->title = 'Câu Hỏi Của Bạn';
		$this->oView->current_link = site_url('nhat-ky-cua-ban/index/cau-hoi-cua-ban');
		
		$rsQuestion = array();
		if (!empty($this->_current_member_id)) 
		{
			$objQuestion = new Amway_Question();
			$rsQuestion = $objQuestion->getRowset("member_id = ? && active = ?",array($this->_current_member_id, 1),"last_update DESC");
		}
		
		$this->oView->rsQuestion = $rsQuestion;
		
		$this->renderView('nhat-ky-cua-ban/index/index');
	}
	
	public function traLoiCuaBanAction()
	{
		$this->oView->title = 'Trả Lời Của Bạn';
		$this->oView->current_link = site_url('nhat-ky-cua-ban/index/tra-loi-cua-ban');
		
		$rsQuestion = array();
		if (!empty($this->_current_member_id)) 
		{
			$objQuestion = new Amway_Question();
			$objComment = new Amway_Comment();
// 			$rsQuestion = $objQuestion->getRowset("member_id = ? && active = ?",array($this->_current_member_id, 1),"last_update DESC");
			
			$rsQuestion = $objQuestion->getQuestionHaveComment($this->_current_member_id);
			
			for($i = 0; $i < count($rsQuestion); $i++)
			{
// 				$rsComment = $objComment->getRowset("question_id = ? && member_id = ? && active = 1",array($rsQuestion[$i]['question_id'],$this->_current_member_id),"last_update DESC");
				$rsComment = $objComment->getAllCommentOfQuestionWithMember($rsQuestion[$i]['question_id'] ,$this->_current_member_id ,TRUE);
				$rsQuestion[$i]['rsComment'] = $rsComment;
			}
			
		}
				
		$this->oView->rsQuestion = $rsQuestion;
		
		$this->renderView('nhat-ky-cua-ban/index/index');
	}
	
	public function addQuestionAction()
	{
		if ($this->oParams->isPost())
		{
			$objQuestion = new Amway_Question();
				
			$cat_id = $this->oParams->varPost("cat_id");
			$data["cat_id"] = $cat_id;
			$data["member_id"] = $this->oParams->varPost("member_id");
			$data["question"] = $this->oParams->varPost("question");
	
			$data['create_at'] = now_to_mysql();
			$last_id = $objQuestion->insert($data);
			
// 			$this->send_notify_email($data);
			
			echo json_encode(array("message" => "Cảm ơn bạn đã gửi thông tin cho Amway! Thông tin của bạn đang được chúng tôi xử lý và sẽ phản hồi cho bạn trong thời gian sớm nhất."));
			exit();			
	
// 			redirect('nhat-ky-cua-ban/index/index');
		}
	
		redirect('nhat-ky-cua-ban/index/index');
	}
	
	public function addCommentAction()
	{
		if ($this->oParams->isPost())
		{
			$objComment = new Amway_Comment();
				
			$cat_id = $this->oParams->varPost("cat_id");
	
			$data["question_id"] = $this->oParams->varPost("question_id");
			$data["member_id"] = $this->oParams->varPost("member_id");
			$data["content"] = $this->oParams->varPost("content");
	
			$data['active'] = 1;
			$data['create_at'] = now_to_mysql();
			$last_id = $objComment->insert($data);
			
			echo json_encode(array("message" => "Cảm ơn bạn đã gửi thông tin cho Amway! Thông tin của bạn đang được chúng tôi xử lý và sẽ phản hồi cho bạn trong thời gian sớm nhất."));
			exit();			
	
// 			redirect('nhat-ky-cua-ban/index/index');
		}
	
		redirect('nhat-ky-cua-ban/index/index');
	}
	
	private function send_notify_email($rowQuestion)
	{
		$arrVal['rowQuestion'] = $rowQuestion;
		$arrVal['fb_user'] = $_SESSION["fb_user"];
	
		$oConfigSys = new Base_ConfigureSystem();
		$rs = $oConfigSys->getGroupConfigure(2);
	
		foreach ($rs as $row)
			$configEmail[$row['code']] = $row['value'];
	
		$email_view = new View();
		$body = $email_view->parser(__LAYOUT_PATH."/email_template/notify_question.phtml",$arrVal);
	
		$email = new Email($this->oConfigureSystem['email_contact'],"Câu hỏi mới",$body);
		$email->connect($configEmail);
		//		$result = @$email->Send();
		$result = $email->Send();
	}
	
}
